<?php
/**
 * Created by PhpStorm.
 * User: aron
 * Date: 2019/5/20
 * Time: 3:34 PM
 */

include_once "../../Util/DebugUtil/DebugUtil.php";

$filename = "./DirOperation.php";

$basename = basename($filename);
echo "basename = $basename";
yt_debug_print_break_line();



/**
 * 遍历删除目录
 * @param $dir 目录
 */
function delDir($dir) {
    if (file_exists($dir)) {
        if ($dirHandle = @opendir($dir)) {
            while ($filename = readdir($dirHandle)) {
                if ($filename == "."
                || $filename == "..") {
                    continue;
                }
                $subFile = $dir . DIRECTORY_SEPARATOR . $filename;
                if (is_file($subFile)) {
                    // unlink删除文件
                    unlink($subFile);
                } else if (is_dir($subFile)) {
                    delDir($subFile);
                }
            }
            // rmdir删除空目录
            closedir($dirHandle);
            rmdir($dir);
        }
    }
}

// TestCase
//// 创建目录
//mkdir("./test/subtest/", 0777, true);
//
//// 删除目录
//delDir("./test");

/**
 * 文件夹复制
 * @param $sourceDir 源文件夹
 * @param $destDir 目的位置
 */
function copyDir($sourceDir, $destDir) {
    if(!is_dir($sourceDir)) {return;}
    if (!file_exists($destDir)) {
        mkdir($destDir, 0777, true);
    }
    $sourceDirHandle = opendir($sourceDir);
    while($fileName = readdir($sourceDirHandle)) {
        if ($fileName == "."
            || $fileName == "..") {
            continue;
        }
        $sourceFileName = $sourceDir . DIRECTORY_SEPARATOR . $fileName;
        $destFileName = $destDir . DIRECTORY_SEPARATOR . $fileName;
        if (is_dir($sourceFileName)) {
            copyDir($sourceFileName, $destFileName);
        } else if (is_file($sourceFileName)) {
            copy($sourceFileName, $destFileName);
        }
    }
    closedir($sourceDirHandle);
}

//// 遍历复制文件夹
//copyDir("/tmp/Developer", "./Developer");
//
////// 删除目录
//delDir("./Developer");
